package com.example.chatmodelsollama;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class OllamaClient {

    private static final String OLLAMA_API_URL = "http://127.0.0.1:8888/api/generate";
  public static void main(String[] args) {
        try {
            // 创建HttpClient实例
            HttpClient client = HttpClient.newHttpClient();

            String jsonRequestBody="{\n" +
                    "    \"model\":\"deepseek-r1:8b\",\n" +
                    "    \"prompt\":\"你是谁\",\n" +
                    "    \"stream\": false\n" +
                    "}";

            // 创建HttpRequest实例
            HttpRequest request = HttpRequest.newBuilder()
                    .uri(URI.create(OLLAMA_API_URL))
                    .header("Content-Type", "application/json")
                    .POST(HttpRequest.BodyPublishers.ofString(jsonRequestBody))
                    .build();

            // 发送请求并获取响应
            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

            // 处理响应
            System.out.println("Response status code: " + response.statusCode());
            System.out.println("Response body: " + response.body());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

